import random
from pymt import *
from pyglet.gl import *
from SelectableObject import *


class SelectionGesture(MTGestureWidget):
    #initializes the gestures that can be recognized
    def init_gdb (self):
        gdb = GestureDatabase()
    
        # Circle
        g = gdb.str_to_gesture('eNqllttqHEcURd/7R6SXiHO//IDzGtAHBMcWxsSJBkmG+O+zqzzqbkOSEkQIBma6dp86a5/L7efLtz9e7j49PL98fXrYfr5+Xmi7/Xjh7f7m+eXp8feH55vtItvtl4tut/944n4+tl1snHOcuzx+/vNlHItxLP/l2C/jqe1S41Tj1DccYNre0Z0Em4pIumoL5fZ8f/PX+JW3dz/RHbWyZVMkpUil6Pb82/v/fAnLvJNun17fkFTc5mRFZgWFT1d1fFsmJdyIIrxjLT4vzr6LXwMXs+a04kNcmqqKuJPYs8rX4jHF81Wc3ajS2zLcvfkkrmGqppZajltkrsVn8rlfxakCEXYZro30utmhbqRVotH4mQL5X6rLNJLwd/UBbqRdNTwqjCFzCh65IiSGiZAb3GsdvEymors8zrJ1tGoSqZ3FI0haQkIbzhrvXYlPpuK7OA6VWYwMmSR7nNSTI6sRvat76NoxMqFKHuoNM0AhBkO8x/9X7JOq9K4+wJVLwc2kqXISl4A/xYiMPLhrKa4Tqh5QTZtV0kzgdXD7wZBXs1SjZBtFuFSfTPVg6hQtrY6sRJfiakctpTZuFoCBJhFvSIxOqHpAdYORUxPSXv6DZbiaETSaA+rJON6gPqHqAdUL2YbX0cHg6D6l/UiJgm5yvUF9QtU+qcMXrt1ehhZ4LtXX/tKEemVBGEt5m1iNl/KjSSRMSq5gju4G56/VJ1Y7YQ2F9VwarhR423d1IRRBGvwo47W+zoxNqnai6sKS1Ggp3ZkRh3gHcMAqGYCasTakTah2gnqNGb7QMTtyF9dizA0UMHojAqA3iE+mdjC1rChCxMXoBNxH5OgxaL7oDLASUpTrQvVJ1E+FyoGxVIlPaoyHg6hzkjBAF5zYzusG5hOoH0AxkUsLZhlj2XOv05EwlC3DkoViM6RnLf59kTj1Xq5sVClzBDROgcM7yQqbq6CaZO1Enzz94MmY1lZXPyoPideUV2UKHCSGG+kbzOKTpx88MTKJUH8ocbTxqDpChzlhok4kpTAUfR16TKDB+7B2wZIRCWBowBx2hK5olZkH1jXQmEBjX5B47CnoitiAxNEk9dB2TJM2VBcyx3N1WmlPnrHvR4Mm+i1mg5OigupUn0zo8di/HPsfpspae/KMfT0SpzGNGp8eKYcyo5tHaZM7YpY1zJgwY9+NBJMM86Cxe5Hx7HpXbUZdQhcUUKSR64TkJJk7SbQUHQ0Ve2/BJnr0FDI0GthSQ3msY7xuhzlJ5k5SsVEVveaF8rTq4pWYS0g3aANr9Mz3+L+/eXn88vD0/s8PD9i70SqmJX7847GWX2P49fL0+PHrh5fxsG/vCtI06xVZb+x5w913fwPv4lI1')
        g.label = 'Circle'
        gdb.add_gesture(g)

        self.gdb = gdb
    
    #constructor    
    def __init__(self):
        super(SelectionGesture, self).__init__()
        self.temp_gestures = {}
        #self.real_gestures = []
        self.real_gestures = {}
        self.init_gdb()
        #modified
        #self.w =w

    
    def on_gesture(self, gesture, x, y):
        touchID = gesture.touchID
        
        best = self.gdb.find(gesture, minscore=0.8)
        
        if not best:
            print 'No gesture found\nString version of your last gesture :\n', self.gdb.gesture_to_str(gesture)
            #return None
        else:
            print 'Gesture found, score', best[0], ':', best[1].label
            result=best[1].label
            
            if result == 'Circle' :    
                #for each connectable obj on the screen
                w = self.get_parent_window()
                for obj in w.children:
                    if isinstance(obj, MTSelectableObject):        
                        print obj
                        if self.contains(self.points[touchID], obj.x, obj.y) :
                            print 'funcionou!!!'
                            obj.select()
                        else:
                            print 'merda!!!'

    #function that defines if a point is inside a given gesture
    #algoritm found here http://www.ariel.com.au/a/python-point-int-poly.html   
    def contains (self, poly, x, y):
        #poly = gesture
        #poly = gesture.strokes[-1].points
        n = len(poly)
        print poly
        inside =False
        
        #p1x, p1=poly[0]
        p1x = poly[0][0]
        p1y = poly[0][1]

        #print 'p1x: ' + str(p1x)
        #print 'pyx: ' + str(p1y)
        
        for i in range(n+1):
            p2x = poly[i%n][0]
            p2y = poly[i%n][1]
            if y > min(p1y,p2y):
                if y <= max(p1y,p2y):
                    if x <= max(p1x,p2x):
                        if p1y != p2y:
                            xinters = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
                        if p1x == p2x or x <= xinters:
                            inside = not inside
            p1x = p2x
            p1y = p2y
    
        return inside

    